<?php

namespace App\Http\Controllers\Cruise\Web;

use App\Http\Controllers\BaseController;
use App\Http\Middleware\OpenShop\UserPermissionAuth;
use App\Http\Validate\Cruise\CruiseProjectValidate;
use App\Http\Validate\Cruise\CruiseTemplateValidate;
use App\Models\Cruise\CruiseCheckSelfTaskModel;
use App\Models\Cruise\CruiseTemplateModel;
use App\Service\Cruise\CruiseProject\CruiseBaseService;
use App\Service\Cruise\CruiseProject\CruiseProjectParam;
use App\Service\Cruise\CruiseProject\CruiseProjectService;
use App\Service\Cruise\CruiseTemplate\CruiseTemplateService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;

class CruiseTemplateController extends BaseController
{
    public function typeLists(Request $request)
    {
        $types = CruiseBaseService::getPermissionTypes(getRmsUserPermissionList([], UserPermissionAuth::MODULE_CRUISE));

        return $this->success(CruiseTemplateService::templateTypeLists($types));
    }

    public function lists(Request $request)
    {
        $name         = (string)$request->get('name');
        $status       = (string)$request->get('status');
        $pageSize     = (int)$request->get('pageSize', 20);
        $pageNum      = (int)$request->get('pageNum', 1);
        $templateType = (int)$request->get('type');
        if ($templateType) {
            $types = [$templateType];
        } else {
            $types = CruiseBaseService::getPermissionTypes(getRmsUserPermissionList([], UserPermissionAuth::MODULE_CRUISE));
        }

        return $this->success((new CruiseTemplateService())->lists($name, $pageNum, $pageSize, $types, $status));
    }

    public function detail(Request $request, int $id)
    {
        return $this->success((new CruiseTemplateService())->setTemplateId($id)->detail());
    }

    public function add(Request $request)
    {
        $templateBase = $request->get('template');
        $positions    = $request->get('positions');
        $userId       = getRmsUserId();

        CruiseTemplateValidate::addValidate($request->all());

        $templateId = (new CruiseTemplateService())
            ->setUserId($userId)
            ->setTemplateId(0)
            ->addOrEdit($templateBase, $positions);

        return $this->success(['templateId' => $templateId]);
    }

    public function edit(Request $request, int $id)
    {
        $templateBase = $request->get('template');
        $positions    = $request->get('positions');
        $userId       = getRmsUserId();

        CruiseTemplateValidate::addValidate($request->all());

        (new CruiseTemplateService())->setUserId($userId)->setTemplateId($id)->addOrEdit($templateBase, $positions);

        return $this->success(['templateId' => $id]);
    }

    public function offline(Request $request, int $id)
    {
        $status = $request->get('status');

        $this->validate($request, [
            'status' => [
                'required',
                'integer',
                Rule::in([CruiseTemplateModel::STATUS_ON, CruiseTemplateModel::STATUS_OFF]),
            ],
        ]);

        (new CruiseTemplateService())->setTemplateId($id)->offline($status);

        return $this->success();
    }

    /**
     * @param Request $request
     * @return JsonResponse
     */
    public function selfCheckTemplateList(Request $request): JsonResponse
    {
        $this->validate($request, [
            'templateType' => 'integer',
            'rule'         => 'integer|' . Rule::in(array_keys(CruiseCheckSelfTaskModel::RULE_TEXT)),
        ]);

        $templateType = $request->get('templateType', CruiseTemplateModel::TYPE_5);
        $rule         = $request->get('rule', CruiseCheckSelfTaskModel::RULE_DAY);

        return $this->success(CruiseTemplateService::selfCheckTemplateList($templateType, $rule));
    }
}
